package com.madou.dbtool.redis.node;


import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.common.messages.DBToolBundle;
import com.madou.dbtool.event.DBEventManager;
import com.madou.dbtool.redis.constants.RedisIds;
import com.madou.dbtool.redis.event.OpenRedisClientEvent;
import com.madou.dbtool.redis.event.OpenRedisDataEvent;
import com.madou.dbtool.redis.event.OpenRedisMonitorEvent;
import com.madou.dbtool.redis.event.OpenRedisSlowLogEvent;
import com.madou.dbtool.redis.manager.RedisNodeInfo;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.redis.utils.BytesToStringUtil;
import com.madou.dbtool.relationship.common.BaseNonRelationNodeParm;
import com.madou.dbtool.relationship.node.menu.NodeMenuFactory;
import com.madou.dbtool.ui.tree.DataSourceNode;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author miukoo
 * Redis数据库信息节点
 */
@HideClass("redisDataBaseNode")
public class RedisDataBaseNode extends BaseNonRelationNodeParm implements RedisTreeNodeInfo {
    //表的菜单
    //表的菜单
    public static List<JMenuItem> REDIS_NODE_MENU = new ArrayList<>();
    static {
        REDIS_NODE_MENU.addAll(DEFAULT_NODE_MENU);
        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.dataview"),68,68,AllIcons.Common.TABLE));
        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.client"),67,67,AllIcons.Common.TERMINAL));
        REDIS_NODE_MENU.add(null);
//        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.monitor"),77,77,AllIcons.Common.QUERY_LOG));
        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.slowlog"),83,83,AllIcons.Common.SLOW_LOG));
    }
    // 资源管理器
    RedisSourceManager redisSourceManager;
    // 数据库信息
    List<String> keys = new ArrayList<>();
    // 当前数据库的索引
    int index;
    // 当前数据库的大小
    Long dbsize = 0L;
    RedisNodeInfo redisNodeInfo;
    public RedisDataBaseNode(){
        super(null);
    }
    public RedisDataBaseNode(DataSourceNode parent, RedisSourceManager redisSourceManager, RedisNodeInfo redisNodeInfo, int index) {
        super(parent);
        this.redisSourceManager = redisSourceManager;
        this.redisNodeInfo = redisNodeInfo;
        this.index = index;
        dbsize = redisSourceManager.getDBSize(index);
    }

    @Override
    public DataSourceNode getChildren(DataSourceNode parent, int idex) {
        return new RedisKeysNode(this,redisSourceManager,keys.get(idex),redisNodeInfo,this.index);
    }

    @Override
    public String getText() {
        return String.format("DB%d (%d)",index,dbsize);
    }

    @Override
    public Icon getIcon() {
        if(open){
            return AllIcons.Common.DATABASE_DARK;
        }
        return AllIcons.Common.DATABASE;
    }
    /**
     * 子元素右键菜单点击
     * @return
     */
    @Override
    public void clickMenu(JTree jTree, TreePath treePath, JMenuItem jMenuItem){
        // 广播事件
        if(isMenuCode(jMenuItem,1)){
            reload(jTree,treePath);
        }
        if(isMenuCode(jMenuItem,67)){
            DBEventManager.notice(new OpenRedisClientEvent(RedisIds.EVENT_ID_CLIENT+""+index,redisSourceManager,redisNodeInfo,index));
        }
        if(isMenuCode(jMenuItem,77)){
            DBEventManager.notice(new OpenRedisMonitorEvent(RedisIds.EVENT_ID_MONITOR,redisSourceManager,redisNodeInfo,index));
        }
        if(isMenuCode(jMenuItem,83)){
            DBEventManager.notice(new OpenRedisSlowLogEvent(RedisIds.EVENT_ID_SLOW,redisSourceManager,redisNodeInfo,index));
        }
        if(isMenuCode(jMenuItem,68)){
            DBEventManager.notice(new OpenRedisDataEvent(RedisIds.EVENT_ID_DATAVIEW+""+index,redisSourceManager,redisNodeInfo,null,index));
        }
    }

    @Override
    public void reload(JTree jTree, TreePath treePath) {
        open=false;
        super.reload(jTree,treePath);
    }


    @Override
    public int countChild() {
        if(!open) {
            keys.clear();;
            List<byte[]> list = redisSourceManager.getFirst(index);
            for (byte[] bytes : list) {
                keys.add(BytesToStringUtil.byte2Hex(bytes));
            }
            dbsize = redisSourceManager.getDBSize(index);
            open = true;
        }
        return keys.size();
    }

    @Override
    public boolean hasChild() {
        return true;
    }

    /**
     * 子元素右键菜单
     * @return
     */
    @Override
    public List<JMenuItem> rightMenu(){
        List temp = new ArrayList();
        for (JMenuItem dbMenu : REDIS_NODE_MENU) {
            temp.add(NodeMenuFactory.copyMenuItem(dbMenu));
        }
        return temp;
    }

    @Override
    public RedisSourceManager getRedisSourceManager() {
        return this.redisSourceManager;
    }

    @Override
    public RedisNodeInfo getRedisNodeInfo() {
        return this.redisNodeInfo;
    }

    @Override
    public int getIndex() {
        return index;
    }

    public void setRedisSourceManager(RedisSourceManager redisSourceManager) {
        this.redisSourceManager = redisSourceManager;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
